package com.elfinder.impl;

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.elfinder.localfs.LocalFsVolume;
import com.elfinder.services.FsService;
import com.elfinder.services.FsServiceFactory;
import com.elfinder.services.FsVolume;

/**
 * A StaticFsServiceFactory always returns one FsService, despite of whatever it
 * is requested
 * 
 * @author bluejoe
 * 
 */
public class StaticFsServiceFactory implements FsServiceFactory {
	FsService _fsService;

	@Override
	public FsService getFileService(HttpServletRequest request, ServletContext servletContext) {
		String userId = request.getParameter("userId");
		DefaultFsService fsService = new DefaultFsService();
		LocalFsVolume fsVolume = new LocalFsVolume();
		fsVolume.setName("文件");
		File file =  new File(request.getSession().getServletContext().getRealPath("/") + "userfiles" + File.separator + userId);
		if(!file.exists()){
			file.mkdirs();
		}
		fsVolume.setRootDir(file);
		fsService.addVolume("File_" + userId, fsVolume);
		fsService.setSecurityChecker(_fsService.getSecurityChecker());
		fsService.setServiceConfig(_fsService.getServiceConfig());
		return fsService;
	}

	public FsService getFsService() {
		return _fsService;
	}

	public void setFsService(FsService fsService) {
		_fsService = fsService;
	}
}
